package net.wangliping.android.tools.microblog;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import net.wangliping.android.tools.R;

public class Settings extends Activity {

	private EditText usrF5Text;
	private EditText pwdF5Text;
	private EditText apiF5Text;
	private EditText usrQQText;
	private EditText pwdQQText;

	private Button okButton;
	private Button cancelButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.microblog_settings);

		usrF5Text = (EditText) findViewById(R.id.f5_username);
		pwdF5Text = (EditText) findViewById(R.id.f5_password);
		apiF5Text = (EditText) findViewById(R.id.f5_api_key);
		usrQQText = (EditText) findViewById(R.id.qq_username);
		pwdQQText = (EditText) findViewById(R.id.qq_password);

		okButton = (Button) findViewById(R.id.settings_ok);
		cancelButton = (Button) findViewById(R.id.settings_cancel);

		read();

		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				save();
				finish();
			}
		});

		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void save() {
		MicroBlogData usr = new MicroBlogData(this);
		usr.updateUsers(MicroBlogData.SITE_F5, usrF5Text.getText().toString(),
				pwdF5Text.getText().toString(), "", apiF5Text.getText()
						.toString(), "true", "");

		usr.updateUsers(MicroBlogData.SITE_QQ, usrQQText.getText().toString(),
				pwdQQText.getText().toString(), "", "", "true", "");

		usr.close();
	}

	private void read() {
		MicroBlogData usr = new MicroBlogData(this);
		HashMap<String, String> map = usr.getUserValues(MicroBlogData.SITE_F5);
		if (map != null && map.size() != 0) {
			usrF5Text.setText(map.get(MicroBlogData.COL_USER));
			pwdF5Text.setText(map.get(MicroBlogData.COL_PWD));
			apiF5Text.setText(map.get(MicroBlogData.COL_API_KEY));
		}

		if (map != null && map.size() != 0) {
			map = usr.getUserValues(MicroBlogData.SITE_QQ);
			usrQQText.setText(map.get(MicroBlogData.COL_USER));
			pwdQQText.setText(map.get(MicroBlogData.COL_PWD));
		}

		usr.close();
	}
}
